package com.jia.lidao.others.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jia.lidao.R;
import com.jia.lidao.RunningContext;
import com.jia.lidao.UserUtils;
import com.jia.lidao.others.BaseFragment;
import com.jia.lidao.others.adapter.ClassifyTabAdapter;
import com.jia.lidao.others.adapter.QuickAdapter;
import com.jia.lidao.others.enity.Classify;
import com.jia.lidao.ui.WebActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 分类tab页的子页
 */
public class ClassifyTabFragment extends BaseFragment {


    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private ClassifyTabAdapter adapter;
    private List<Classify.CategoriesBean.ChildBean>  data;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("HQQ"," onCreate");
    }

    @Override
    protected int getInflateLayout() {
        return R.layout.fragment_classify_tab;
    }


    @Override
    protected void setUpView(View view) {
        Log.e("HQQ"," setUpView");
        adapter = new ClassifyTabAdapter(getContext());
        data = new ArrayList<>();
        adapter.setData(data);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Classify.CategoriesBean.ChildBean>(){
            @Override
            public void OnItemClick(Classify.CategoriesBean.ChildBean bean, int postion) {
//                String params = "firstproductdetail"+"?goods_id="+bean.getId()+"&area_id="+ RunningContext.AREA_CODE +"&latitude="+RunningContext.mLatitude
//                        +"&longitude="+RunningContext.mLongitude;
                String params="firstlinelist?"+"second_category_id="+bean.getId()+"&area_id="+ UserUtils.getInstance()
                        .getAgent().area.id +"&latitude="+RunningContext.mLatitude
                        +"&longitude="+RunningContext.mLongitude;
                toWebView(params);
            }
        });
        Classify.CategoriesBean classifyTabView  = ( Classify.CategoriesBean)getArguments().getSerializable("been");
        update(classifyTabView);
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.e("HQQ"," onResume");
    }

    public void update(Classify.CategoriesBean  data){

        if(null != data){
           String name = data.getName();
            tvTitle.setText(name);
            Picasso.with(getContext()).load(data.getPic()).into(ivTitle);
            List<Classify.CategoriesBean.ChildBean> childBeen = data.getChild();
            if(null != childBeen){
                 adapter.reflashData(childBeen);
            }

        }
    }

    private void toWebView(String param) {
        Intent intent = new Intent(getContext(), WebActivity.class);
        intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, param);
        startActivity(intent);
    }
}
